import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Batch {
	//201521123091
	public static void compare() throws SQLException {
		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
		String[] names = new String[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
		java.sql.Statement statement = connection.createStatement();
		statement.executeUpdate("CREATE TABLE IF NOT EXISTS students (id int, name char(10))");
		java.sql.PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO students VALUES (?, ?)");
		for (int i = 0; i < 1000; i++) {
			preparedStatement.setInt(1, i);
			preparedStatement.setString(2, names[i % 10]);
			preparedStatement.executeUpdate();
		}
//		ResultSet resultSet = statement.executeQuery("SELECT * FROM students");
//		while (resultSet.next()) {
//			System.out.println(resultSet.getInt("id") + " " + resultSet.getString("name"));
//		}
		connection.close();
	}
	
	
	
	
	//201521123091
	public static void solve() throws SQLException {
		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
		connection.setAutoCommit(false);
		String[] names = new String[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
		
		java.sql.Statement statement = connection.createStatement();
		statement.executeUpdate("CREATE TABLE IF NOT EXISTS students (id int, name char(10))");
		java.sql.PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO students VALUES (?, ?)");
		for (int i = 0; i < 1000; i++) {
			preparedStatement.setInt(1, i);
			preparedStatement.setString(2, names[i % 10]);
			preparedStatement.addBatch();
		}
		
		preparedStatement.executeBatch();
		
//		ResultSet resultSet = statement.executeQuery("SELECT * FROM students");
//		while (resultSet.next()) {
//			System.out.println(resultSet.getInt("id") + " " + resultSet.getString("name"));
//		}
		connection.close();
	}

	public static void main(String[] args) throws SQLException {
		// TODO Auto-generated method stub
		solve();

	}

}
